import os.path
from pathlib import Path

import yaml

# 配置下载选项
ydl_opts = {
    'format': '0',
    'outtmpl': '222_2.%(ext)s',        # 输出文件名格式
    'n_threads': 8,                                      # 8 个线程分段下载
    'ratelimit': None,                                   # 无速度限制
}

savePath = ""
videosPath = ""
posterPath = ""

def get_video_path():
    return videosPath
def get_poster_path():
    return posterPath


def getDownloadOptions(name):
    opt = dict(ydl_opts)
    opt["outtmpl"] = f"{os.path.join(videosPath,name)}.%(ext)s"
    return opt

def parse_config_yml():
    global savePath
    cwd = Path.cwd()
    print()
    try:
        with cwd.joinpath("config.yml").open() as file:
            config = yaml.load(file, Loader=yaml.FullLoader)
            savePath = config["savePath"]

    except (FileNotFoundError,yaml.YAMLError) as e:
        print(e)
        savePath = cwd.joinpath("video_download")
    finally:
        mkall_dir()

# 创建所有需要的目录
def mkall_dir():
    Path(savePath).mkdir(parents=True, exist_ok=True)
    videos = Path(savePath).joinpath("videos")
    videos.mkdir(parents=True, exist_ok=True)
    poster = Path(savePath).joinpath("poster")
    poster.mkdir(parents=True, exist_ok=True)
    global videosPath
    global posterPath
    videosPath = str(videos)
    posterPath = str(poster)
    print(videosPath)
    print(posterPath)


